// pages/mail/mailorder.js
Page({
  mixins: [require('../mixin/themeChanged')],

  /**
   * 页面的初始数据
   */

  data: {
    
    city:"",
    showmark: 1,
    showmark2: 0,
    name: "",
    phone: "",
    region: ["请选择", "请选择", "请选择"],
    address: "",
    detail: "",
    list: [],
    currentindex: ""

  },

  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
    console.log(this.data.region)
  },




  bindAddressShow: function (e) {
    var that = this
    var url = wx.getStorageSync("config")

    console.log(wx.getStorageSync('openid'))
    wx.request({
      url: url.service.homeUrl + '/mail/getaddresslist',
      data: {
        openid: wx.getStorageSync('openid'),
      },

      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {

        this.setData({
          list: res.data
        })
        console.log(res.data)
        this.setData({
          showmark: 0,
          showmark2: 1
        })
      },
      fail: function (res) {},
      complete: function (res) {},
    })

  },

  bindChose: function (e) {

    console.log(e.currentTarget.dataset.address)

    this.setData({

      region: e.currentTarget.dataset.address,
      name: e.currentTarget.dataset.name,
      phone: e.currentTarget.dataset.phone,
      detail: e.currentTarget.dataset.detail,
      showmark: 1,
      showmark2: 0

    })
    console.log(this.data.name)
  },

  getname: function (e) {
    this.setData({
      name: e.detail.value
    })
    console.log(this.data.name)
  },

  getphone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  getdetail:function(e){
    this.setData({
      detail:e.detail.value
    })
  },

  bindMarkChange: function (e) {
    var that = this
    var url = wx.getStorageSync("config")
    var list1 =[]
    list1=this.data.list
    list1.splice(e.detail.value,1)
    console.log(list1)
    this.setData({
      list:list1
    })
    //得到当前点击数据的id号
    this.setData({
      currentid: e.currentTarget.dataset.id,
      currentindex:e.currentTarget.dataset.index
    })
    console.log(e.currentTarget.dataset.index)


    wx.showModal({
      title: '删除确认',
      content: '请核对删除内容是否正确',
      success: function (res) {
        if (res.confirm) {


          //请求体
          wx.request({
            url: url.service.homeUrl + '/mail/changeaddressmark',
            data: {
              id: that.data.currentid,
              mark: 0
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            success: (res) => {

              wx.showToast({
                title: res.data.msg,
              });

              wx.request({
                url: url.service.homeUrl + '/mail/getaddresslist',
                data: {
                  openid: wx.getStorageSync('openid'),
                },
          
                method: 'GET',
                dataType: 'json',
                responseType: 'text',
                success: (res) => {
          
                  that.setData({
                    list: res.data
                  })
                
                },
                fail: function (res) {},
                complete: function (res) {},
              })
                      
            },
            fail: function (res) {
              wx.showToast({
                title: res.data.msg,
              })
            },
            complete: function (res) {
              wx.showToast({
                title: res.data.msg,
              })
            },
          })
        }
      }
    })
  },


  postAddress:function(){
    var that=this
    var openid =wx.getStorageSync('openid')
    var url = wx.getStorageSync("config") 
    wx.request({
      url: url.service.homeUrl+'/mail/postaddress',
      data: { 
        address:that.data.region[0]+"-"+that.data.region[1]+"-"+that.data.region[2],
        detail:that.data.detail,
        name:that.data.name,
        phone:that.data.phone, 
        openid:openid          
            },
      // header: {
      //   Authorization: userInfo ? "token " + userInfo.token : ""
      // },
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        wx.navigateTo({url: '../mail/mailorder',})
        wx.showToast({
          title: res.data.msg,
          icon:"none"
        }) 
       
      },
      fail: function (res) {
        wx.showToast({
          title: res.data.msg,
        }) 
       },
      complete: function (res) { },
    })
  },





  postOrder:function(){
    var that=this
    var uid =wx.getStorageSync('uid')
    var url = wx.getStorageSync("config") 
    wx.request({
      url: url.service.homeUrl+'/mail/postmailorder',
      data: { 
        address:that.data.region[0]+"-"+that.data.region[1]+"-"+that.data.region[2],
        detail:that.data.detail,
        name:that.data.name,
        phone:that.data.phone, 
        uid:uid,
        city:that.data.city,
            },
      // header: {
      //   Authorization: userInfo ? "token " + userInfo.token : ""
      // },
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        // wx.navigateTo({url: '../mail/mailorder',})
        wx.showToast({
          title: res.data.msg,
          icon:"none"
        }) 
        
      },
      fail: function (res) {
        wx.showToast({
          title: res.data.msg,
        }) 
       },
      complete: function (res) { },
    })
  },





  changeTheme: function() {
    console.log(this.data);
    getApp().themeChanged(this.data.theme === 'light' ? 'dark' : 'light');
},



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      city: wx.getStorageSync('city')
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})